#include <iostream>
#include <pthread.h>
#include <sys/types.h>
#include <unistd.h>
#include <ctime>
#include "ringQueue.hpp"

void* consumer(void* args)
{
    RingQueue<int> *rq = static_cast<RingQueue<int> *>(args);
    // 消费数据
    while(true)
    {
        sleep(1);
        int data = 0;
        rq->pop(data);
        std::cout << "消费者获得数据: " << data << std::endl;
    }
}

void* productor(void* args)
{
    RingQueue<int> *rq = static_cast<RingQueue<int> *>(args);
    // 生产数据
    while(true)
    {
        int data = rand() % 10 + 1;
        rq->push(data);
        std::cout << "生产者生产数据: " << data << std::endl;
    }
}

int main()
{
    srand(time(nullptr) ^ getpid());
    // 环形队列
    RingQueue<int> *rq = new RingQueue<int>();

    // 创建两个线程，单消费者、单生产者
    pthread_t c[3], p[3];
    for (int i = 0; i < 3; ++i)
        pthread_create(c + i, nullptr, consumer, rq);
    for (int i = 0; i < 3; ++i)
        pthread_create(p + i, nullptr, productor, rq);

    for (int i = 0; i < 3; ++i)
        pthread_join(c[i], nullptr);
    for (int i = 0; i < 3; ++i)
        pthread_join(p[i], nullptr);

    delete rq;
    return 0;
}